package com.siro.pm.ui.main;

import com.siro.pm.I18nManager;
import com.siro.pm.model.Project;
import com.siro.pm.model.provider.ProjectProvider;
import com.siro.pm.ui.detail.ProjectDetailPage;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since ProjectPage.java v1.0
 */
public class ProjectPage extends VerticalLayout {
	private static final long serialVersionUID = 5805653924799037155L;

	ProjectPage self;

	private Button button_AddProject;
	private Button button_DeleteProject;
	public Table projectsTable;

	public ProjectPage() {
		self = this;
	}

	public void createContents() {
		setSizeFull();
		setSpacing(true);
		setMargin(true, true, true, true);

		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);
		// Add Project
		button_AddProject = new Button(I18nManager.getMessage("button.add"));
		button_AddProject.addListener(new ClickListener() {
			private static final long serialVersionUID = -5620810406204435724L;

			public void buttonClick(ClickEvent event) {
				ProjectDetailPage projectDetailPage = new ProjectDetailPage();
				projectDetailPage.setProjectPage(self);
				projectDetailPage.setProject(null);
				projectDetailPage.createContents();

				Window projectDetailWindow = new Window(I18nManager
						.getMessage("project.info"));
				projectDetailWindow.setModal(true);
				projectDetailWindow.setWidth("800px");
				projectDetailWindow.setHeight("80%");
				projectDetailPage.setProjectWindow(projectDetailWindow);
				projectDetailWindow.addComponent(projectDetailPage);
				getWindow().addWindow(projectDetailWindow);

			}
		});
		buttons.addComponent(button_AddProject);
		buttons.setComponentAlignment(button_AddProject, Alignment.TOP_RIGHT);

		// Delete Project
		button_DeleteProject = new Button(
				I18nManager.getMessage("button.delete"));
		// Developer user = (Developer) PmApplication.getDefault().getUser();
		// if (user.getRole().equals(DeveloperProvider.DEVELOPER_ROLE_DATA[1]))
		// {
		// button_DeleteProject.setEnabled(false);
		// }
		button_DeleteProject.addListener(new ClickListener() {
			private static final long serialVersionUID = -5620810406204435724L;

			public void buttonClick(ClickEvent event) {
				Object object = projectsTable.getValue();
				if (object != null) {
					Project project = (Project) object;
					Project.deleteProject(project);
					projectsTable.removeItem(project);
				}
			}
		});
		buttons.addComponent(button_DeleteProject);
		buttons.setComponentAlignment(button_DeleteProject, Alignment.TOP_RIGHT);

		addComponent(buttons);
		setComponentAlignment(buttons, Alignment.TOP_RIGHT);

		// Documents Table
		projectsTable = new Table();
		projectsTable.setWidth("100%");
		projectsTable.setHeight("100%");
		projectsTable.setSelectable(true);
		projectsTable.setImmediate(true);
		projectsTable.setColumnWidth(
				ProjectProvider.PROJECT_PROPERTY_PROJECTNAME, 100);
		// projectsTable.setColumnWidth(
		// ProjectProvider.PROJECT_PROPERTY_PROJECTMANAGER, 100);
		// projectsTable.setColumnWidth(ProjectProvider.PROJECT_PROPERTY_STATUS,
		// 100);
		// projectsTable.setColumnWidth(
		// ProjectProvider.PROJECT_PROPERTY_STARTTIME, 100);
		projectsTable.setContainerDataSource(ProjectProvider
				.getProjectData(null));
		// turn on column reordering and collapsing
		projectsTable.setColumnReorderingAllowed(true);
		projectsTable.setColumnCollapsingAllowed(true);
		addComponent(projectsTable);
		setExpandRatio(projectsTable, 1);

	}
}